/*
 * @Author: Baishui
 * @Date: 2021-09-16 17:52:30
 * @LastEditors: Baishui
 * @LastEditTime: 2021-10-08 17:58:19
 * @Description: file content
 */

import { reqMenuList } from '@/api/app'
import { defineStore } from 'pinia'
import { store } from '../index'
import { routeHandler } from '@/router/utils'
import { RouteRecordRaw } from 'vue-router'
import router from '@/router'
interface IState {
  theme: string
  collapsed: boolean
  menuList: any[]
}

export const useAppStore = defineStore({
  id: 'app',
  state: (): IState => ({
    theme: 'light',
    collapsed: false,
    menuList: [],
  }),
  getters: {
    getTheme(): string {
      return this.theme
    },
  },
  actions: {
    setTheme(theme: string): void {
      this.theme = theme
    },

    setCollapsed(collapsed: boolean): void {
      this.collapsed = collapsed
    },

    async getMenuList(): Promise<any> {
      const res = await reqMenuList()
      this.menuList = res || []
      const routes = routeHandler(res)
      this.addRoute(routes)
    },

    addRoute(routes: RouteRecordRaw[]) {
      routes.forEach((route) => {
        router.addRoute(route)
      })
    },
  },
})

export function useAppStoreWithOut() {
  return useAppStore(store)
}
